package com.lizemin.product.controller;

import cn.hutool.json.JSONUtil;
import com.lizemin.client.OrderClient;
import com.lizemin.client.ProductClient;
import com.lizemin.common.ApiResponse;
import com.lizemin.req.BuyProductReq;
import com.lizemin.req.CreateOrderReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lzm
 * @date 2025/4/17 22:10
 * @description
 */
@Slf4j
@RestController
public class ProductController implements ProductClient {

    @Autowired
    private OrderClient orderClient;

    @Override
    public ApiResponse buyProduct(BuyProductReq req) {
        log.info("开始购买商品");
        CreateOrderReq createOrderReq = new CreateOrderReq();
        createOrderReq.setProductId(req.getProductId());
        createOrderReq.setCount(req.getCount());
        ApiResponse order = orderClient.createOrder(createOrderReq);
        log.info("调用订单服务后返回的结果为：{}", JSONUtil.toJsonStr(order));
        return ApiResponse.success();
    }

    @Override
    public ApiResponse getOrders() {
        log.info("代理对象class为：{}", orderClient.getClass().getName());
        log.info("代理对象为：{}", orderClient);
        log.info("开始查询订单");
        ApiResponse order = orderClient.listOrders();
        log.info("订单信息为为：{}", JSONUtil.toJsonStr(order));
        return order;
    }
}
